package br.com.unifebe.tcc.componente.GUI;

import java.awt.event.KeyEvent;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.text.DateFormatter;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;
import javax.swing.text.NumberFormatter;

import br.com.unifebe.tcc.componente.GUI.GUIUtils.ShowCaseType;
import br.com.unifebe.tcc.componente.GUI.GUIUtils.Tipos;

@SuppressWarnings("serial")
public class JEditText extends JFormattedTextField{

	private int maxlength=0;
	private ShowCaseType showcase=ShowCaseType.FREE_CASE;
	private JLabel msgLabel;
	private boolean isKey=false;
	private Tipos tipo_dado = Tipos.STRING;
	private char[] acepted_keys;
	private String atributo;
	private String valorPadrao;
	private Object definindoTexto = new Object();
	
	public JEditText(String s) {
		if(s != null && !s.equals("")){
			setMaskFormatter(s);
		}
	}

	public JEditText(Class clazz) {
		setMaskFormatter(clazz);
	}

	
	private void init() {
		FormataEdit();
		this.setBackground(Parametros.getCorEdit());
		this.setFont(Parametros.getFontEdit());
		this.setForeground(Parametros.getCorFontEdit());
		
		this.addFocusListener(new java.awt.event.FocusListener() {
			@Override
			public void focusGained(java.awt.event.FocusEvent e) {
				SwingUtilities.invokeLater(new Runnable() {
					@Override
					public void run() {
						JEditText.this.setBackground(Parametros.getCorEditSelecionado());
						JEditText.this.setFont(Parametros.getFontEditSelecionado());
						JEditText.this.setForeground(Parametros.getCorFontEditSelecionado());
						JEditText.this.selectAll();
					}
				});
			}
			@Override
			public void focusLost(java.awt.event.FocusEvent e) {
				SwingUtilities.invokeLater(new Runnable() {
					@Override
					public void run() {
						JEditText.this.setBackground(Parametros.getCorEdit());
						JEditText.this.setFont(Parametros.getFontEdit());
						JEditText.this.setForeground(Parametros.getCorFontEdit());
					}
				});
			}
		});

		this.addKeyListener(new java.awt.event.KeyAdapter() {
			@Override
			public void keyTyped(java.awt.event.KeyEvent e) {
				
				if (! tipo_dado.equals(Tipos.STRING)) {
					if (acepted_keys!=null && !acepted_keys.equals("") 
							&& Arrays.binarySearch(acepted_keys, e.getKeyChar()) < 0) {
						
						e.setKeyChar((char)KeyEvent.VK_CLEAR);
					
					} else {
						if (getShowCase()==ShowCaseType.UPPER_CASE) {
							e.setKeyChar(Character.toUpperCase(e.getKeyChar()));
						}
						if (getShowCase()==ShowCaseType.LOWER_CASE) {
							e.setKeyChar(Character.toUpperCase(e.getKeyChar()));
						}
						if ((getText().length()>=getMaxLength()) &&
						   (getMaxLength()>0)) {
							e.setKeyChar((char)KeyEvent.VK_CLEAR);
						}
					}
				} else {
					if (getShowCase()==ShowCaseType.UPPER_CASE) {
						e.setKeyChar(Character.toUpperCase(e.getKeyChar()));
					}
					if (getShowCase()==ShowCaseType.LOWER_CASE) {
						e.setKeyChar(Character.toUpperCase(e.getKeyChar()));
					}
					if ((getText().length()>=getMaxLength()) &&
					   (getMaxLength()>0)) {
						e.setKeyChar((char)KeyEvent.VK_CLEAR);
					}
				}
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				if ( e.getKeyCode() == 10 ) {
					JComponent comp = ( (JComponent) GUIUtils.findNextFocus() ); 
					if (comp != null) {
						comp.grabFocus();
					}
				}
			}
			
		});
	}
	
	public void setMaskFormatter(String s ){
		MaskFormatter formatter = null;
		try {
			formatter = new MaskFormatter(s);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		setFormatterFactory(new DefaultFormatterFactory(formatter));
	}

	public void setMaskFormatter(Class clazz ){
		if(clazz.getName().equals(BigDecimal.class.getName())){
			setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter()));
		}else if (clazz.getName().equals(BigDecimal.class.getName())){
			setFormatterFactory(new DefaultFormatterFactory(new DateFormatter()));
		}

	}

	public BigDecimal getBigDecimalValue(){
		String s = super.getText().trim();
		return new BigDecimal(s);
	}
	public Date getDateValue(){
		 Calendar.getInstance().set(10, 8, 1990);
		String s = super.getText().trim();
		String[] sb = s.split("/");
		return Calendar.getInstance().getTime();
	}
	
	private void FormataEdit() {
		switch (tipo_dado) {
			case DATE:
				{
					this.setHorizontalAlignment(SwingConstants.LEFT);
					this.setText("");
					this.acepted_keys = Parametros.getNumerosInteirosLiberados();
					Arrays.sort(acepted_keys);
					break;
				}
			case INTEGER:
				{
					this.setHorizontalAlignment(SwingConstants.RIGHT);
					this.setText("0");
					this.acepted_keys = Parametros.getNumerosInteirosLiberados();
					Arrays.sort(acepted_keys);
					break;
				}
			case LONG:
				{
					this.setHorizontalAlignment(SwingConstants.RIGHT);
					this.setText("0");
					this.acepted_keys = Parametros.getNumerosInteirosLiberados();
					Arrays.sort(acepted_keys);
					break;
				}
			case FLOAT:
				{
					this.setHorizontalAlignment(SwingConstants.RIGHT);
					this.acepted_keys = Parametros.getNumerosFloatsLiberados();
					Arrays.sort(acepted_keys);
					this.setText("0");
					break;
				}
			case STRING: 
				{
					this.setHorizontalAlignment(SwingConstants.LEFT);
					this.setText("");
					this.acepted_keys = Parametros.getCharsLiberados();
					Arrays.sort(acepted_keys);
					break;
				}
			default:
				break;
		}
	}
	
	
	public boolean isKey() {
		return this.isKey;
	}
	
	public void setKey(boolean b) {
		this.isKey = b;
	}
	
	public void setTipo(Tipos tipo) {
		this.tipo_dado = tipo;
		FormataEdit();
	}

	public Tipos getTipo() {
		return this.tipo_dado;
	}
	public int getMaxLength() {
		return maxlength;
	}
	
	public void setMaxLength(int length) {
		this.maxlength = length;
	}
	
	public void setShowCase(ShowCaseType showcase) {
		this.showcase = showcase;
	}
	
	public ShowCaseType getShowCase() {
		return this.showcase;
	}
	
	public void setMsgLabel(JLabel label) {
		this.msgLabel = label;
	}
	public JLabel getMsgLabel() {
		return this.msgLabel;
	}
	
}
